﻿//********************************************************************************//
//创建日期：<创建日期，2016-03-30>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：地下水打印账单
//********************************************************************************//
layui.use(['layer', 'form', 'element', 'laydate', 'table', 'jquery'], function () {
    var layer = layui.layer
    , form = layui.form
    , element = layui.element
    , laydate = layui.laydate
    , table = layui.table
    , $ = layui.jquery;

    laydate.render({
        elem: "#c2PrintDate"
    });
    laydate.render({
        elem: "#c3PrintDate"
    });
    var table1 = table.render({
        elem: "#ContentList1",
        url: "",
        height: 'full-200',
        page: true,
        limit: 100,
        limits: [100],
        id: 'table1',
        cols: [[
            { checkbox: true, fixed: "left" }
            , { field: 'S_YONGHUH', title: '用户号', width: 90, align: 'center', fixed: "left" }
             , { field: 'S_YONGHUM', title: '用户名',width: 200, align: 'left', fixed: "left", edit: true }
              , { field: 'I_YM_STR', title: '水量年月', sort: true, width: 100, align: 'center', fixed: "left" }
              , { field: 'N_JINE_STR', title: '金额',minWidth: 90, align: 'right', fixed: "left" }
              , { field: 'S_KAIHUHM', title: '开户户名', width: 200, align: 'left', edit: true, fixed: "left" }
           , { field: 'S_KAIHUYH', title: '开户银行',  minWidth: 180, align: 'left' }
           , { field: 'S_KAIHUZH', title: '开户账号', minWidth: 150, align: 'left' }
           , { field: 'I_ZHUANGTAI', title: '打印', templet: '#iDYZT', width: 65, align: 'center' }
           , { field: 'S_FAPIAOHM', title: '发票号码', minWidth: 110, align: 'center', edit: true, fixed: "right" }
            
        ]]
    });
    var table2 = table.render({
        elem: "#ContentList2",
        url: "",
        height: 'full-165',
        page: true,
        limit: 100,
        limits: [100],
        id: 'table2',
        cols: [[
            { checkbox: true, fixed: "left" }
           , { field: 'S_FAPIAOHM', title: '发票号码', sort: true, minWidth: 110, align: 'center' }
            , { field: 'I_ZHUANGTAI', title: '状态', templet: '<div>{{d.I_ZHUANGTAI==1?"成功":"打坏"}}</div>', sort: true, minWidth: 70, align: 'center' }
            , { field: 'D_DAYINRQ_STR', title: '打印日期', sort: true, minWidth: 110, align: 'center' }
           , { field: 'S_ZHANGDANBH', title: '费用编号', sort: true, minWidth: 100, align: 'center' }
            , { field: 'I_YM_STR', title: '水量年月', sort: true, minWidth: 100, align: 'center' }
            , { field: 'S_YONGHUH', title: '用户号', sort: true, minWidth: 100, align: 'center' }
            , { field: 'S_YONGHUM', title: '用户名', sort: true, minWidth: 250, align: 'center' }
            , { field: 'N_JINE_STR', title: '金额', sort: true, sort: true, minWidth: 100, align: 'right' }
            , { field: 'N_SHUIJIA_STR', title: '水价', sort: true, sort: true, minWidth: 70, align: 'right' }
            , { field: 'I_YSL_STR', title: '水量', sort: true, sort: true, minWidth: 100, align: 'right' }
        ]]
    });
    initinfo();
    /*initial*/
    function initinfo() {
        //显示当前发票号码
        $("#lsBillNo").text(getFaPiaoHM(1));
        var date = new Date()
        //$("#c2PrintDate").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
        //$("#c3PrintDate").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
    }
    function getFaPiaoHM(count) {
        var fphms = "";
        //显示当前打印发票号码
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSZW/FaPiao.ashx",
            data: {
                "op": "FiaoPiaoHMHQ",
                "count": count
            },
            dataType: "json",
            success: function (rs) {
                if (rs.State) {
                    fphms = rs.Data1;
                }
                else {
                    layer.alert(rs.Msg);
                }
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        return fphms;
    }
    /*end initial*/

    /*event*/
    $("#oSearch").click(function () {
        if ($("#cYHH").val() == "") {
            layer.alert("请输入用户号");
            return;
        }
        if ($("#cY1").val() == "" || $("#cY2").val() == "") {
            layer.alert("请输入水量年区间");
            return;
        }
        load1();
    });
    $("#oReset").click(function () {
        $("#cYHH").val("");
        $("#ContentList1 .datarow").remove();
        $("#lsCount").text("0");
        $("#lsMoney").text("0.00");
    });
    $("#o2Search").click(function () {
        load2();
    });

    $("#o3Search").click(function () {
        var printdate = $("#c3PrintDate").val();
        load3(printdate);
    });
    $("#o3Clear").click(function () {
        $("#ContentList3 .datarow").remove();
        $("#ls3Count").text(0);
    });

    $("#btnPrint").click(function () {
        //判断是否有可用发票
        if ($("#lsBillNo").text() == "") {
            layer.alert("无可用发票");
            return;
        }
        var checks = table.checkStatus('table1');
        if (checks.data.length == 0) {
            layer.alert("请勾选要打印的账单");
            return;
        }
        var sffs=$("#oiSFFS").val();
        if (sffs=="-1")
        {
            layer.alert("请选择收费方式");
            return;
        }
        //if (checks.data.length > 1) {
        //    layer.alert("当前一次仅能打印一张账单");
        //    return;
        //}
        //要打印的账单资料完整度验证
        for (var i = 0; i < checks.data.length; i++) {
            if (checks.data[i].S_KAIHUHM == "") {
                layer.alert("请完善开户户名");
                return;
            }
        }
        //取得报表参数 
        var zdbhs = new Array();
        var yhhs = new Array();
        var yhms = new Array();
        var khhms = new Array();
        var fphms = new Array();
        var fphmsGet = getFaPiaoHM(checks.data.length).split(',');
        var j = 0;
        for (var i = 0; i < checks.data.length; i++) {
            zdbhs.push(checks.data[i].S_ZHANGDANBH);
            yhhs.push(checks.data[i].S_YONGHUH);
            yhms.push(checks.data[i].S_YONGHUM);
            khhms.push(checks.data[i].S_KAIHUHM);
            if (checks.data[i].S_FAPIAOHM == "") {
                fphms.push(fphmsGet[j]);
                j++;
            }
            else {
                fphms.push(checks.data[i].S_FAPIAOHM);
            }
        }
        var params = new Array();
        params[0] = "V_ZHANGDANBHS:" + zdbhs.join(',');
        params[1] = "V_YONGHUMS:" + escape(yhms.join(','));
        params[2] = "V_KAIHUHMS:" + escape(khhms.join(','));
        params[3] = "V_FAPIAOHMS:" + fphms.join(',');
        params[4] = "V_SHOUFEIFS:" + sffs;
        wap.common.printjx('ZhangDanDY', [
            { key: 'QUERYPARAM', value: params.join("|") },
            { key: 'BAOBIAOID', value: 100202 },
            { key: 'USERINFO', value: "csms" }
        ]);
        var dyjg = "";
        layer.alert("请确认打印状态!", {
            closeBtn: false,
            btn: ['成功', '打坏', '未打印'],
            btn1: function (index) {
                $.ajax({
                    type: "post",
                    cache: false,
                    async: false,
                    url: "/ashx/QSSFZW/ZhangDan.ashx",
                    data: {
                        "op": "DY",
                        "dyzt": "success",
                        "zdbhs": zdbhs.join(','),
                        "yhhs": yhhs.join(','),
                        "yhms": yhms.join(','),
                        "khhms": khhms.join(','),
                        "fphms": fphms.join(','),
                        "sffs": sffs
                    },
                    dataType: "json",
                    success: function (rs) {
                        if (rs.State) {
                            //更新行打印状态记录值
                            for (var i = 0; i < checks.data.length; i++) {
                                checks.data[i].I_ZHUANGTAI = 1;
                                checks.data[i].S_FAPIAOHM = fphms[i];
                            }
                            table1.reload();
                            //重新加载下一张发票号码
                            $("#lsBillNo").text(getFaPiaoHM(1));
                        }
                        else {
                            layer.alert(rs.Msg);
                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText);
                    }
                });
                layer.close(index);
            },
            btn2: function (index) {
                $.ajax({
                    type: "post",
                    cache: false,
                    async: false,
                    url: "/ashx/QSSFZW/ZhangDan.ashx",
                    data: {
                        "op": "DY",
                        "dyzt": "broken",
                        "zdbhs": zdbhs.join(','),
                        "yhhs": yhhs.join(','),
                        "yhms": yhms.join(','),
                        "khhms": khhms.join(','),
                        "fphms": fphms.join(','),
                        "sffs": sffs
                    },
                    dataType: "json",
                    success: function (rs) {
                        if (rs.State) {
                            //重新加载下一张发票号码
                            $("#lsBillNo").text(getFaPiaoHM(1));
                        }
                        else {
                            layer.alert(rs.Msg);
                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText);
                    }
                });
                layer.close(index);
            },
            btn3: function (index) {

            }
        });
    });
    $("#btnPrintSaving").click(function () {
        var checks = table.checkStatus('table1');
        if (checks.data.length == 0) {
            layer.alert("请勾选要打印存根的账单");
            return;
        }
        //取得报表参数 
        var zdbhs = new Array();
        var j = 0;
        for (var i = 0; i < checks.data.length; i++) {
            zdbhs.push(checks.data[i].S_ZHANGDANBH);
        }
        var params = new Array();
        params[0] = "V_ZHANGDANBH:" + zdbhs.join(',');
        wap.common.printjx('ZhangDanDY', [
            { key: 'QUERYPARAM', value: params.join("|") },
            { key: 'BAOBIAOID', value: 100210 },
            { key: 'USERINFO', value: "csms" }
        ]);
    });
    $("#o2Operate").click(function () {
        var checks = table.checkStatus('table2');
        if (checks.data.length == 0) {
            layer.alert("请勾选要操作的项");
            return;
        }
        //获取选中发票号码
        var bhsArr = new Array();
        var zdbhsArr = new Array();
        for (var i = 0; i < checks.data.length; i++) {
            bhsArr.push(checks.data[i].S_FAPIAOHM);
            zdbhsArr.push(checks.data[i].S_ZHANGDANBH);
        }
        layer.alert("将选中发票打印状态变更为:", {
            btn: ['成功', '打坏', '未打印'],
            btn1: function (index) {
                //同一账单不能有两条以上成功打印记录
                for (var i = 0; i < zdbhsArr.length; i++) {
                    for (var j = 0; j < zdbhsArr.length; j++) {
                        if (i != j && zdbhsArr[i] == zdbhsArr[j]) {
                            layer.alert("同一账单不能有两条以上成功打印记录");
                            layer.close(index);
                            return;
                        }
                    }
                }
                $.ajax({
                    type: "post",
                    cache: false,
                    async: false,
                    url: "/ashx/QSSFZW/ZhangDan.ashx",
                    data: {
                        "op": "DXS_DYZTBG",
                        "dyzt": 1,
                        "fphms": bhsArr.join(",")
                    },
                    dataType: "json",
                    success: function (rs) {
                        if (rs.State) {
                            //更新打印状态显示
                            //未变更的排除
                            var bhsT = rs.Data1.split(",");
                            var exists = false;
                            for (var i = 0; i < bhsArr.length; i++) {
                                for (var j = 0; j < bhsT.length; j++) {
                                    if (bhsArr[i] == bhsT[j]) {
                                        exists = true;
                                        break;
                                    }
                                }
                                if (!exists) {
                                    checks.data[i].I_ZHUANGTAI = 1;
                                }
                            }
                            table2.reload();
                            $("#lsBillNo").text(getFaPiaoHM(1));
                        }
                        layer.alert(rs.Msg);
                    },
                    error: function (error) {
                        layer.msg(error.responseText);
                    }
                });
                layer.close(index);
            },
            btn2: function (index) {
                $.ajax({
                    type: "post",
                    cache: false,
                    async: false,
                    url: "/ashx/QSSFZW/ZhangDan.ashx",
                    data: {
                        "op": "DXS_DYZTBG",
                        "dyzt": 2,
                        "fphms": bhsArr.join(",")
                    },
                    dataType: "json",
                    success: function (rs) {
                        if (rs.State) {
                            //更新打印状态显示
                            for (var i = 0; i < bhsArr.length; i++) {
                                checks.data[i].I_ZHUANGTAI = 1;
                            }
                            table2.reload();
                            $("#lsBillNo").text(getFaPiaoHM(1));
                        }
                        else {
                            layer.alert(rs.Msg);
                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText);
                    }
                });
                layer.close(index);
            },
            btn3: function (index) {
                $.ajax({
                    type: "post",
                    cache: false,
                    async: false,
                    url: "/ashx/QSSFZW/ZhangDan.ashx",
                    data: {
                        "op": "DXS_DYZTBG",
                        "dyzt": 0,
                        "fphms": bhsArr.join(",")
                    },
                    dataType: "json",
                    success: function (rs) {
                        if (rs.State) {
                            //更新打印状态显示
                            for (var i = 0; i < bhsArr.length; i++) {

                            }
                            load2();
                            $("#lsBillNo").text(getFaPiaoHM(1));
                        }
                        else {
                            layer.alert(rs.Msg);
                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText);
                    }
                });
                layer.close(index);
            }
        });

    });

    
    /*event end*/

    /*list*/    
   
    function bindOperate() {        
        //设置补打和打印启禁用
        if ($("input[name='cType']:checked").val() == "0") {
            $("#btnPrintSaving").attr("disabled", "disabled");
            $("#btnPrint").removeAttr("disabled");
            $("#btnPrintSaving").css("cursor", "default");
            $("#btnPrintSaving").css("border-color", "#C9C9C9");
            $("#btnPrint").css("cursor", "pointer");
            $("#btnPrint").css("border-color", "#009688");
            $("#btnPrint").show();
            $("#btnPrintSaving").hide();
        }
        else {
            $("#btnPrintSaving").removeAttr("disabled");
            $("#btnPrint").attr("disabled", "disabled");
            $("#btnPrintSaving").css("cursor", "pointer");
            $("#btnPrintSaving").css("border-color", "#009688");
            $("#btnPrint").css("cursor", "default");
            $("#btnPrint").css("border-color", "#C9C9C9");
            $("#btnPrint").hide();
            $("#btnPrintSaving").show();
        }
        //form.render("input");
    }

    function load1() {
        table1.reload({
            url: "/ashx/QSSFZW/ZhangDan.ashx",
            where: {
                "op": "ZDXX",
                "type": $("input[name='cType']:checked").val(),
                "yhh": $("#cYHH").val(),
                "y1": $("#cY1").val(),
                "y2": $("#cY2").val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }

        });
        bindOperate();
    }
    function load2() {
        var checkboxs = $("[name='c2Printed']:checked");
        if (checkboxs.length == 0) {
            layer.alert("至少选择一种打印状态");
            return;
        }
        var ym = $("#c2YM").val() == "" ? "0" : $("#c2YM").val();
        //2种选中情况  1:已打印未打印均选中 查询全部用2表示  2:查询1种(1:已打印 0:未打印)
        var zt = (checkboxs.length == 2) ? 0 : (checkboxs.length == 1) ? $(checkboxs[0]).val() : -1;
        var hm = $("#c2HM").val();
        var printdate = $("#c2PrintDate").val();
        table2.reload({
            url: "/ashx/QSSFZW/ZhangDan.ashx",
            where: {
                "op": "DXS_YDLIST",
                "zt": zt,
                "ym": ym == "" ? 0 : ym,
                "hm": hm,
                "printdate": printdate
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }

        });
    }

    function load3(printdate) {
        var index = layer.load(1);
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/QSSFZW/ZhangDan.ashx",
            data: {
                "op": "DXS_DYTJ",
                "printdate": printdate
            },
            dataType: "json",
            success: function (rs) {
                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStr3(rs[i]);
                }
                $("#ls3Count").text(rs.length);
                $("#ContentList3").find(".datarow").remove();
                $("#ContentList3").append(strs);
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        layer.close(index);
    }
    function createItemStr3(obj) {
        var str = $("#ItemStr3 tr").prop("outerHTML");
        var itemstr, qshm, jshm, sl, dyrq;
        qshm = obj["S_QISHIFPHM"];
        jshm = obj["S_JIESHUFPHM"];
        sl = obj["I_SL"];
        dyrq = obj["D_DAYINRQ"]["DateString"];
        itemstr = str.replace(new RegExp("objQISHIFPHM", "gm"), qshm)
                        .replace(new RegExp("objJIESHUFPHM", "gm"), jshm)
                        .replace(new RegExp("objSL", "gm"), sl)
                        .replace(new RegExp("objDaYinRQ", "gm"), dyrq)
        ;
        return itemstr;
    }
    /*end list*/

    /*logic*/
    function verify() {
        return true;
    }

    /*logic end*/

    // exports('DaYinZD', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});